type Callback = (...args: any[]) => void;

class EventEmiter {
  private events: Record<string, Set<Callback>> = {};
  private buffer: Record<string, any> = {};

  on(event: string, callback: Callback) {
    if (!this.events[event]) {
      this.events[event] = new Set();
    }
    this.events[event].add(callback);

    if (this.buffer[event]) {
      callback(...this.buffer[event]);
    }

    return () => {
      this.events[event].delete(callback);
    };
  }

  emit(event: string, ...args: any[]) {
    this.buffer[event] = args;
    if (this.events[event]) {
      this.events[event].forEach((callback) => {
        callback(...args);
      });
    }
  }
}

export default EventEmiter;

